#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2020/9/3 16:03
# @Author : wn 
# @File: selfTradeChannel.py
# @SoftWare: PyCharm
# @Site:
# @Table:
# ｡◕‿◕｡｡◕‿◕｡｡◕‿◕｡
from django.shortcuts import render, HttpResponse
from django.views import View

from Manager.utils.mypage import *
from Manager.utils.pageSearch import *


class SelfTradeConfig(View):
    def get(self, request):
        # 当前在第几页
        off = request.GET.get('offset', None)
        # 一页几条
        lim = request.GET.get('limit', None)
        # get请求修改页面字段值
        get_selftrade_modify = request.GET.get('modify_channelid', None)
        if lim is None and get_selftrade_modify is not None:
            # 策略表
            st_list = list(Selftradechannel.objects.filter(channelid=get_selftrade_modify))  # [StragegyObj,]
            for item in st_list:
                result_dict = {}
                result_dict['channelid'] = item.channelid  # str
            result = my_page([result_dict], lim, off)
            return JsonResponse(result, safe=False)

        if lim is None:
            html = "selfTradeChannel.html"
            channel_list = Channel.objects.all()
            return render(request, html, {'url': '/strategy/selftrade', 'channel_list': channel_list})

    def post(self, request):
        if request.POST.get('selftrade_search', None):
            # 初始化页面数据，搜索
            jsonRes = search_selftradesearch()
            return jsonRes
        elif request.POST.get('del_DB', None):
            id = request.POST.get('id', None)  # str
            print(id, len(id), type(id))
            s_obj = Selftradechannel.objects.filter(channelid=id)
            s_obj.delete()
            jsonRes = search_selftradesearch()
            return jsonRes
        elif request.POST.get('addORmodify_DB', None):
            id = request.POST.get('channelid', None)
            aORmFlag = request.POST.get('aORmFlag', None)
            if aORmFlag == 'add':
                # 新增
                if Selftradechannel.objects.filter(channelid=id).exists():
                    # return HttpResponse('add is repeat')
                    return HttpResponse('ae')
                else:
                    Selftradechannel.objects.create(
                        channelid=id
                    )
                    jsonRes = search_selftradesearch()
                    return jsonRes
            elif aORmFlag == 'modify':
                # 修改
                if Selftradechannel.objects.filter(channelid=id).exists():
                    print(123)
                    return HttpResponse('me')
                else:
                    print(2344)
                    Selftradechannel.objects.create(
                        channelid=id
                    )
                jsonRes = search_selftradesearch()
                return jsonRes
